//app.js
var util = require('utils/util.js');
import 'umtrack-wx';
App({
    umengConfig: {
        appKey: '5fd0c2b1bed37e4506c7ba9b', //由友盟分配的APP_KEY
        useOpenid: true, // 是否使用openid进行统计，此项为false时将使用友盟+随机ID进行用户统计。使用openid来统计微信小程序的用户，会使统计的指标更为准确，对系统准确性要求高的应用推荐使用OpenID。
        autoGetOpenid: true, // 是否需要通过友盟后台获取openid，如若需要，请到友盟后台设置appId及secret
        debug: false, //是否打开调试模式
        uploadUserInfo: false // 自动上传用户信息，设为false取消上传，默认为false
    },
    onLaunch: function(opt) {
        var randomNum1 = Math.ceil(Math.random() * 10); // 主要获取1到10的随机整数，取0的几率极小。
        if (randomNum1 % 2 == 0) {
            [this.globalData.url.urltest2, this.globalData.url.host2, this.globalData.url.urltest1, this.globalData.url.host1] = [this.globalData.url.urltest1, this.globalData.url.host1, this.globalData.url.urltest2, this.globalData.url.host2]
        }
        this.globalData.pathDetail = opt;
        // 获取用户的手机型号
        wx.getSystemInfo({
                success: (res) => {
                    this.globalData.textraData.customData.clientInfo = res.system;
                    this.globalData.textraData.customData.clientVersion = res.version;
                }
            })
            // 获取本程序的appid
            // let accountInfo = wx.getAccountInfoSync().miniProgram;
        var city = wx.getStorageSync('city');
        util.getUrl(this.globalData.url).then((res) => {
            // 登录
            wx.login({
                success: res => {
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                    let loginData = {
                        action: "login",
                        // appid: accountInfo.appId,
                        appid: "wx2efc0705600eb6db",
                        code: res.code,
                    };
                    util.request('user.php', loginData, 'POST').then(res => {
                        if (res.data.data) {
                            wx.setStorageSync("openid", res.data.data.openid)
                            wx.setStorageSync("session_key", res.data.data.session_key)
                            wx.setStorageSync("unionid", res.data.data.unionid)
                        }
                    })
                }
            })
        })
    },

    // 监听页面错误
    onError(err) {
        // 报错页面分析
        wx.reportAnalytics('check_article', {
            id: Number(this.globalData.pathDetail.query.id),
            type: this.globalData.pathDetail.query.type,
            city: this.globalData.pathDetail.query.city,
            scene: this.globalData.pathDetail.scene,
            path: this.globalData.pathDetail.path,
            err_reason: err,
        });
    },

    // 监听页面不存在
    onPageNotFound(res) {
        wx.redirectTo({
            url: 'pages/index/index',
        })
    },

    globalData: {
        platform: "wx",
        pathDetail: "",
        extConfig: null,
        openid: "",
        version: "21.08.26",
        showTap: true,
        tAppid: "wx8abaf00ee8c3202e",
        textraData: {
            id: '152540',
            customData: {
                clientInfo: "",
                clientVersion: "",
            }
        },

        shareImage: "",
        shareTitle: "",
        api: 'https://api1.bendibao.com/banshitong/index.php',
        apibak: 'https://api.bendibao.com/banshitong/index.php',
        url: {
            urltest1: "https://wxapidg.bendibao.com/apitest.php",
            urltest2: "https://wxapisz.bendibao.com/apitest.php",
            host1: "https://wxapidg.bendibao.com",
            host2: "https://wxapisz.bendibao.com",
        },
        // url: {
        //     urltest1: "https://wxapidg.bendibao.com/apitest.php",
        //     urltest2: "https://wxapisz.bendibao.com/apitest.php",
        //     host1: "https://szapi.bendibao.com",
        //     host2: "https://szapi.bendibao.com",
        // },
    }
})